import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.example.idlefish.R

class HistoryAdapter(
    private val items: List<String>, // 数据源
    private val onItemClick: (Int) -> Unit, // 点击事件回调
    private val recyclerView: RecyclerView
) : RecyclerView.Adapter<HistoryAdapter.HistoryViewHolder>() {

    // ViewHolder 定义
    class HistoryViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        val textView: TextView = itemView.findViewById(R.id.ht_item) // 假设布局中有个 TextView
    }

    // 创建 ViewHolder
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): HistoryViewHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.item_history, parent, false) // 替换为你的 item 布局文件
        return HistoryViewHolder(view)
    }

    // 绑定数据到 ViewHolder
    override fun onBindViewHolder(holder: HistoryViewHolder, position: Int) {
        val item = items[position]
        holder.textView.text = item
        holder.itemView.setOnClickListener {
            onItemClick(position) // 调用点击事件回调
        }
    }

    // 获取数据项数量
    override fun getItemCount(): Int = items.size
}
